﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.ComponentModel;
using BesAsm.Stormwater.Swmm5Results;

namespace BesAsm.Stormwater.Swmm5ResultsUi
{
  public class TextSearchFilter
  {
    public TextSearchFilter(ICollectionView view, TextBox textBox)
    {
      string filterText = string.Empty;

      view.Filter = delegate(object o)
      {
        if (string.IsNullOrEmpty(filterText))
          return true;

        IName namedObject = o as IName;
        if (namedObject == null) 
          return false;

        string s = namedObject.Name;

        if (string.IsNullOrEmpty(s))
          return false;

        int i = s.IndexOf(filterText, 0, StringComparison.InvariantCultureIgnoreCase);

        return i > -1;
      };

      textBox.TextChanged += delegate
      {
        filterText = textBox.Text;
        view.Refresh();
      };
    }

  }
}
